//
//  SocietyViewController.m
//  Product_NEWS
//
//  Created by dllo on 15/12/28.
//  Copyright © 2015年 FCH. All rights reserved.
//

#import "SocietyViewController.h"
#import "AFNetworking.h"
#import "MJRefresh.h"
#import "SocietyDetailViewController.h"
#import "SocietyViewCell.h"
@interface SocietyViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *myTableView;
@property(nonatomic,assign)BOOL isUpLoading;
@property(nonatomic,assign)NSInteger page;
@property(nonatomic,retain)SocietyViewCell *cell;
@property(nonatomic,retain)UIAlertView *alert;
@property(nonatomic,retain)NSMutableArray *modelArray;
@end

@implementation SocietyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.modelArray = [NSMutableArray array];
    self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 * SCREEN_HEIGHT) style:UITableViewStylePlain];
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    [self.view addSubview:self.myTableView];
    [_myTableView release];
    
    self.isUpLoading = NO;
    self.page = 1;
    [self addHeader];
    [self addFooter];

}



-(void)addHeader
{
    __block SocietyViewController *vc = self;
    
    [self.myTableView addHeaderWithCallback:^{
        
        vc.page = 1;
        vc.isUpLoading = NO;
        [vc GetPersonList:vc.page];
    }];
    
    
    [self.myTableView headerBeginRefreshing];
    
}


-(void)addFooter
{
    __block SocietyViewController *vc = self;
    
    
    [self.myTableView addFooterWithCallback:^{
        vc.page ++;
        vc.isUpLoading  =YES;
        [vc GetPersonList:vc.page];
        
    }];
}

-(void)GetPersonList:(NSInteger)nextCursor{
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    

    NSString *url_string = [NSString stringWithFormat:@"http://www.tingwen.me/akcms_category.php?id=10&page=%ld",self.page];
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        NSMutableArray *allArray = [NSMutableArray array];
        
        allArray  = responseObject;
        
        if (self.isUpLoading == NO) {
            //说明是下拉，就要清空数组中的数据
            [self.modelArray removeAllObjects];
        }
        
        for (NSDictionary *dic  in allArray) {
            
            SocietyModel *model = [[SocietyModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            
            [self.modelArray addObject:model];
            
        }
        
        
        [self.myTableView footerEndRefreshing];
        [self.myTableView headerEndRefreshing];
        [self.myTableView reloadData];
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.myTableView headerEndRefreshing];
        [self.myTableView footerEndRefreshing];
        
        self.alert =[ [UIAlertView alloc]initWithTitle:@"网络不给力" message:@"加载失败" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [self.alert show];
        [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(timeAction:) userInfo:nil repeats:NO];
        
    }];
    
    
}

-(void)timeAction:(NSTimer *)time
{
    [self.alert dismissWithClickedButtonIndex:0 animated:YES];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.modelArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentifier = nil;
    SocietyViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (!cell) {
        cell = [[SocietyViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    
    
    SocietyModel *model = [self.modelArray objectAtIndex:indexPath.row];
    
    cell.model = model;
    
    return cell;
    
}


//cell点击 进入详情
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    SocietyDetailViewController *detail = [[SocietyDetailViewController alloc]init];
    SocietyModel *model = [[SocietyModel alloc]init];
    model = [self.modelArray objectAtIndex:indexPath.row];
    
    detail.model = model;
    
    [self.navigationController pushViewController:detail animated:YES];
    
    
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100 * SCREEN_HEIGHT;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
